//
//  LKUserInforTool.m
//  YLHelper
//
//  Created by she1323 on 2018/5/25.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKUserInforTool.h"
#import "YLPushViewModel.h"
#define LKAccountPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.archive"]


@implementation LKUserInforTool

+ (void)saveAccount:(LKUserInfor *)account
{
    [NSKeyedArchiver archiveRootObject:account toFile:LKAccountPath];
    
}

+ (LKUserInfor *)account
{
    @try {
        return [NSKeyedUnarchiver unarchiveObjectWithFile:LKAccountPath];
    } @catch (NSException *exception) {
        return nil;
    } @finally {
        
    }
//    return [NSKeyedUnarchiver unarchiveObjectWithFile:LKAccountPath];
}

+ (void)noAccount
{
    [YLPushViewModel clearPushInfo];
    NSFileManager *manager = [NSFileManager defaultManager];
    [manager removeItemAtPath:LKAccountPath error:nil];
}

+ (NSDictionary *)header
{
    if (![LKUserInforTool account].token) {
        return nil;
    }
    return @{@"Authorization" : [LKUserInforTool account].token};
}

+(void)reloadUserInformation{
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:UserDetailUrl header:[LKUserInforTool header] parameters:nil progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        LKUserInfor *userInfor = [LKUserInfor accountWithDict:responseObject[@"result"]];
        userInfor.token = [LKUserInforTool account].token;
        [LKUserInforTool saveAccount:userInfor];
        
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
@end
